//
//  JJJVistorView.swift
//  myBlog
//
//  Created by 姜军君 on 15/11/28.
//  Copyright © 2015年 JJJ. All rights reserved.
//

import UIKit

/// 访客视图协议
protocol vistorViewDelegate:NSObjectProtocol {
    
    // 即将登录
    func vistorViewWillLogin()
    // 即将注册
    func vistorViewWillRegister()
}

class JJJVistorView: UIView {
    
    // 定义代理属性
    weak var delegate:vistorViewDelegate?
    
    // MARK: - 按钮监听方法
    /// 登录
    func loginBottonClicked() {
        delegate?.vistorViewWillLogin()
    }
    
    /// 注册
    func registerBottonClicked() {
        delegate?.vistorViewWillRegister()
    }

    // MARK: - 界面初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置视图
        setupVistorView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //SB开发
//        fatalError("init(coder:) has not been implemented")
        
        // 设置视图
        setupVistorView()
    }
    
    // MARK: - 设置视图
    /// 设置视图内容
    func setupDetailView(isHome:Bool, iconName:String, message:String){
        
        isHome ? startAnim() : sendSubviewToBack(maskImageView)
        
        iconImage.image = UIImage(named: iconName)
        homeImage.hidden = !isHome
        messageLab.text = message
    }
    
    /// 转动转轮
    private func startAnim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        
        anim.removedOnCompletion = false
        
        iconImage.layer.addAnimation(anim, forKey: nil)
    }
    
    /// 设置视图
    private func setupVistorView() {
        
        // 1.添加控件
        addSubview(iconImage)
        addSubview(maskImageView)
        addSubview(homeImage)
        addSubview(messageLab)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.布局控件
        // 转轮
        iconImage.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        // 遮罩视图
        maskImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskImageView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskImageView, "regButton": registerButton]))
        // 小房子
        homeImage.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 文本
        messageLab.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        // 注册
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        // 登录
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }
    
    // MARK: - 懒加载控件
    // 转轮
    lazy private var iconImage:UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return iv
        }()
    // 遮罩视图
    lazy private var maskImageView:UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv
        }()
    // 小房子
    lazy private var homeImage:UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return iv
        }()
    // 文本
    lazy private var messageLab:UILabel = {
        
        let lab = UILabel()
        
        lab.textColor = UIColor.lightGrayColor()
        lab.numberOfLines = 0
        lab.font = UIFont.systemFontOfSize(14)
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        lab.sizeToFit()
        
        return lab
        }()
    // 注册
    lazy private var registerButton:UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "registerBottonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    // 登录
    lazy private var loginButton:UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "loginBottonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()

}
